import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from 'views/home/Home'
import Exclusive from 'views/exclusive/Exclusive'
import Personal from 'views/personal/Personal'
import Login from 'views/login/Login'
import Category from 'views/category/Category'
import Article from 'views/article/Article'
import Register from 'views/register/Register'
import ShopCart from 'views/shopcart/ShopCart'
import Browse from 'views/browse/Browse'
import Search from 'views/search/Search'
import Product from 'views/product/Product'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'home',
    meta: { title:'首页' }
  },
  {
    name: 'home',
    path: '/home',
    component: Home,
    meta: { title:'首页' }
  },
  {
    name: 'exclusive',
    path: '/exclusive',
    component: Exclusive,
    meta: { title:'专属' }
  },
  {
    name: 'personal',
    path: '/personal',
    component: Personal,
    meta: { title:'个人' }
  },
  {
    name: 'login',
    path: '/login',
    component: Login,
    meta: { title:'登录' }
  },
  {
    name: 'category',
    path: '/category',
    component: Category,
    meta: { title:'栏目管理' }
  },
  {
    name: 'article',
    path: '/article',
    component: Article,
    meta: { title:'文章管理' }
  },
  {
    name: 'register',
    path: '/register',
    component: Register,
    meta: { title:'注册' }
  },
  {
    name: 'shopcart',
    path: '/shopcart',
    component: ShopCart,
    meta: { title:'购物车' }
  },
  {
    name: 'browse',
    path: '/browse',
    component: Browse,
    meta: { title:'浏览' }
  },
  {
    name: 'search',
    path: '/search',
    component: Search,
    meta: { title:'搜索' }
  },
  {
    name: 'product',
    path: '/product/:id',
    component: Product,
    meta: { title:'文章详情' }
  }
]

const router = new VueRouter({
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 当路由发生改变时, 修改页面的 title
  if(to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router
